package com.zerosky.crm.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zerosky.crm.po.Employee;
import com.zerosky.crm.po.Registers;
import com.zerosky.crm.service.RegistersService;
import com.zerosky.crm.util.BaseAction;
import com.zerosky.crm.util.MyFileUploadUtils;
import com.zerosky.crm.util.PageInfo;

/**
 * 签到相关业务的Controller
 * 
 * @author 钟璟
 *
 */
@Controller
public class RegisterController extends BaseAction {
	private Logger logger = LoggerFactory.getLogger(RegisterController.class); // 日志

	@Resource(name = "registersService")
	private RegistersService registerService;

	/**
	 * 跳转签到列表界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/toRegisterList.do")
	public ModelAndView toCustomer(String id) throws Exception {
		// if (id == null || id == "") {// 处理非法请求
		// return new ModelAndView("/WEB-INF/jsp/error/404");
		// }
		return new ModelAndView("WEB-INF/jsp/office/listRegister");
	}

	/**
	 * 拜访定位列表
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/listRegister.do")
	@ResponseBody
	public Map<String, Object> regisstersList(HttpServletRequest request, HttpSession session,
			HttpServletResponse response, Integer limit, Integer page, String registerEmp, String individual,
			String deptId, String branchId, String teamId) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		Employee user = (Employee) session.getAttribute("loginUser");// 获取登录用户信息
		Map<String, Object> param = new HashMap<String, Object>();// 定义条件集合
		PageInfo<Registers> pageInfo = new PageInfo<Registers>();
		try {
			pageInfo.setPageNo(page);
			pageInfo.setPageSize(limit);
			if (registerEmp != null && registerEmp != "") {
				param.put("registerEmp", registerEmp);
			}
			if (individual.equals("individual")) {
				param.put("registerEmpId", user.getEmployeeId());
			} else {
				if (user.getQuarters().equals("1")) {// 团队长
					param.put("teamId", teamId);
				} else if (user.getQuarters().equals("2")) {// 部门经理
					param.put("branchId", branchId);
					param.put("teamId", teamId);
				} else {// 公司负责人
					param.put("deptId", deptId);
					param.put("branchId", branchId);
					param.put("teamId", teamId);
				}

			}
			PageInfo<Registers> pageRole = registerService.getRegistersPageInfoList(param, pageInfo);
			json.put("total", pageRole.getPageCount());
			json.put("data", pageRole.getList());
			json.put("flag", 1);

		} catch (Exception e) {
			json.put("flag", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 签到记录
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/getRegissters.do")
	@ResponseBody
	public Map<String, Object> getRegissters(HttpServletRequest request, HttpSession session,
			HttpServletResponse response, Integer id) throws Exception {
		Map<String, Object> json = new HashMap<String, Object>(); // 定义返回map集合
		try {
			Registers register = registerService.getRegistersById(id);
			if (register != null) {
				json.put("register", register);
				json.put("code", 1);
			} else {
				json.put("code", 0);
			}
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 保存签到记录
	 * 
	 * @param request
	 * @param response
	 * @param Chance
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/saveRegister.do")
	@ResponseBody
	public Map<String, Object> saveOrUpdateChance(HttpServletRequest request, HttpServletResponse response,
			Registers register) throws Exception {
		Employee emp = (Employee) request.getSession().getAttribute("loginUser");
		DateFormat dateFormat = new SimpleDateFormat("yyyyMMdd");
		Map<String, Object> json = new HashMap<>();
		String strName = emp.getName() + dateFormat.format(new Date()).toString() + "拜访记录";
		register.setRegisterDate(new Date());
		register.setRegisterEmp(strName);
		register.setDeptId(emp.getDept().getDeptId());
		register.setBranchId(emp.getBranch() != null ? emp.getBranch().getBranchId() : null);
		register.setTeamId(emp.getTeam() != null ? emp.getTeam().getBranchId() : null);
		register.setRegisterEmpId(emp.getEmployeeId());
		try {
			if (register.getRegisterImgFile() != null) {
				String path = "saituImage/";
				if (MyFileUploadUtils.saveFile(request, register.getRegisterImgFile(), path)) {
					register.setRegisterPhoto(path + request.getAttribute("picture").toString());
				}
			}
			registerService.save(register);
			json.put("code", 1);
		} catch (Exception e) {
			json.put("code", -1);
			logger.error("获取数据失败，原因：" + e);
		}
		return json;
	}

	/**
	 * 跳转签到界面
	 * 
	 * @param request
	 * @param response
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/toEmployeeRegister.do")
	public ModelAndView addActivity(String id, HttpServletRequest request, HttpSession session,
			HttpServletResponse response) throws Exception {
		return new ModelAndView("WEB-INF/jsp/office/employeeRegister");
	}

}
